package com.cw.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.cw.demo.model.Job;
import com.cw.demo.service.JobService;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findJob")
public class JobController extends HttpServlet {

    //private DeptService deptService = new DeptServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JobService jobService = (JobService) WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean("jobService");

        List<Job> jobList = jobService.findByParam(null);
        //将结果返回回去
        //如果要将对象类型转换为json格式，需要添加一个工具类：fastJson
        resp.setContentType("text/plain");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write(JSONObject.toJSONString(jobList));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
